package com.zjh.photoshopstudy.app.register;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.base.Request;
import com.zjh.photoshopstudy.Model.AppUserEntity;
import com.zjh.photoshopstudy.app.register.RegisterPresent.RegisterCallBack;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import static com.zjh.photoshopstudy.Utils.Data.webRoot_URL;

class RegisterHttp {
    private static final int REGISTER_SUCCESS = 1;
    private static final int REGISTER_FAIL = 0;

    public void register(final RegisterCallBack callBack, final String username, final String password) {
        String url = webRoot_URL + "/appUser/add.html";

        OkGo.<String>get(url)//
                .tag(this)//
                .params("uAccountName", username)//
                .params("uPassword", password)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if (map.get("message").equals("添加成功")) {
                            Map<String, Object> map2 = (Map<String, Object>) map.get("data");
                            AppUserEntity userInfo = new AppUserEntity();
                            userInfo.setId(((Double) map2.get("id")).longValue());
                            userInfo.setuName("PSstudent");
                            userInfo.setuAccountName(username);
                            userInfo.setuPassword(password);
                            userInfo.setuIcon("https://tx-free-imgs.acfun.cn/o_1dsjhpr6ucpcfkq1hu1k1t2d50.gif?imageView2/1/w/180/h/100|imageslim");
                            userInfo.setuSex(1);
                            DateFormat bf = new SimpleDateFormat("yyyy-MM-dd");
                            try {
                                userInfo.setuBirthday(bf.parse("1990-01-01"));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            callBack.onRegisterSuccess(userInfo);
                        } else {
                            callBack.onRegisterFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });


    }

    public void checkUser(final RegisterPresent.CheckUserNameCallBack checkUserNameCallBack, String username) {
        String url = webRoot_URL + "/appUser/withoutAuth/validateAccountName.html";
        OkGo.<Boolean>get(url)//
                .tag(this)//
                .params("uAccountName", username)//
                .execute(new Callback<Boolean>() {
                    @Override
                    public void onStart(Request<Boolean, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<Boolean> response) {
                        if (response.body()) {
                            checkUserNameCallBack.checkSuccess();
                        } else {
                            checkUserNameCallBack.checkFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<Boolean> response) {

                    }

                    @Override
                    public void onError(Response<Boolean> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public Boolean convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string().equals("true");
                    }
                });
    }
}
